{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3e9b0b3a",
   "metadata": {},
   "source": [
    "购物车程序项目：\n",
    "\n",
    "要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "     \n",
    "    2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "    3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "    4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "eb5765cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：85\n",
      "------------【商品列表】-------------\n",
      "商品编号:0,商品名称:珍珠奶茶小杯,商品的价格:7元\n",
      "商品编号:1,商品名称:大杯珍珠奶茶,商品的价格:8元\n",
      "商品编号:2,商品名称:加大杯珍珠奶茶,商品的价格:9元\n",
      "商品编号:3,商品名称:1L珍珠奶茶,商品的价格:12元\n",
      "商品编号:4,商品名称:桶装珍珠奶茶,商品的价格:20元\n",
      "请输入正确的商品编号(输入“q”可退出购买)：4\n"
     ]
    },
    {
     "ename": "SyntaxError",
     "evalue": "'break' outside loop (<ipython-input-15-4a4bd682d545>, line 46)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-15-4a4bd682d545>\"\u001b[1;36m, line \u001b[1;32m46\u001b[0m\n\u001b[1;33m    break\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m 'break' outside loop\n"
     ]
    }
   ],
   "source": [
    "    # 商品列表\n",
    "    goods_list = [\n",
    "        #商品1 商品名称、商品价格、商品编号…\n",
    "        #商品模板：[商品名称，商品名称]\n",
    "        [\"珍珠奶茶小杯\",7],\n",
    "        [\"大杯珍珠奶茶\",8.5],\n",
    "        [\"加大杯珍珠奶茶\",9.5],\n",
    "        [\"1L珍珠奶茶\",12],\n",
    "        [\"桶装珍珠奶茶\",20],\n",
    "    ]\n",
    "\n",
    "    #不是所有的代码都可以一次性考虑全面！例如这里应该写一个用户商品列表\n",
    "    user_shopping_cart = [] #append 可以新增列表\n",
    "\n",
    "    #用户输入：input\n",
    "    Alipay = int(input(\"请输入您的余额：\"))\n",
    "    #打印商品列表（规范打印，样式是先写的，然后再输入数据）\n",
    "    #先设计模板样式：商品编号：xx，商品名称：xx，商品的价格：xx元\n",
    "    print(\"------------【商品列表】-------------\")\n",
    "    for i in range(len(goods_list)):\n",
    "         #print(\"商品编号:%s,商品名称:%s,商品的价格:%d元\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "        print(\"商品编号:%s,商品名称:%s,商品的价格:%d元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "\n",
    "    #2、让用户输入商品编号进行商品的购买。\n",
    "    choice =input(\"请输入正确的商品编号(输入“q”可退出购买)：\") # 用户输入可能不是数值，也可能是\n",
    "    #是正确的编号\n",
    "    if choice.isdigit():\n",
    "        #用int转换数据类型为数值\n",
    "        choice = int(choice)\n",
    "        if choice < len(goods_list):      #商品的信息：goods_list[choice],商品的名称：goods_list[choice][0],商品价格：goods_list[choice][1]\n",
    "        #1、看一看用户钱够不够：Alipay VS goods_list[choice][1]\n",
    "            if Alipay < goods_list[choice][1]:\n",
    "                print(\"您的余额不够，请更换支付方式\")\n",
    "        #2、如果够的话，赶紧扣钱，把商品加到购物车\n",
    "            else:\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                user_shopping_cart.append(goods_list[choice])\n",
    "                #提示用户：本次购买成功\n",
    "                print(\"【%s】已经添加到您的购物车，您的余额还有【%d】元\"%(goods_list[choice][0],Alipay))\n",
    "         #不是正确的编号\n",
    "        else:\n",
    "            print(\"您输入的商品不存在，请输入正确的编号\")\n",
    "    elif choice == \"q\":\n",
    "        print(\"-------------------\",'\\n',\"您的购物车：%s\"%(user_shopping_cart),'\\n',\"您的余额还有：%d元\"%(Alipay))\n",
    "        break\n",
    "        #两种情况：\n",
    "        #1、按照我们的要求输入的文本，例如：输入q表示quit退出的意思\n",
    "        #2、输入的不符合我们要求的文本\n",
    "    else:\n",
    "            print(\"请输入正确的商品编号！或者输入“q”退出\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "476b6cb5",
   "metadata": {},
   "source": [
    "* 如何解决星号：尝试中断内核，尝试重启内核"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ccf39dd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
